package com.lzb.musicplayer.receiver;

import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

/**
 * 蓝牙连接状态广播接收器
 */
public class BluetoothReceiver extends BroadcastReceiver {
    
    private OnBluetoothStateChangedListener listener;
    
    public interface OnBluetoothStateChangedListener {
        /**
         * 蓝牙设备已连接
         */
        void onBluetoothConnected();
        
        /**
         * 蓝牙设备已断开
         */
        void onBluetoothDisconnected();
    }
    
    public void setOnBluetoothStateChangedListener(OnBluetoothStateChangedListener listener) {
        this.listener = listener;
    }
    
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        
        if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
            // A2DP连接成功（音频设备连接）
            if (listener != null) {
                listener.onBluetoothConnected();
            }
        } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
            // A2DP断开连接（音频设备断开）
            if (listener != null) {
                listener.onBluetoothDisconnected();
            }
        } else if (BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {
            // 蓝牙连接状态改变
            int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, BluetoothAdapter.ERROR);
            int previousState = intent.getIntExtra(BluetoothAdapter.EXTRA_PREVIOUS_CONNECTION_STATE, BluetoothAdapter.ERROR);
            
            if (state == BluetoothAdapter.STATE_CONNECTED) {
                // 蓝牙已连接
                if (listener != null) {
                    listener.onBluetoothConnected();
                }
            } else if (state == BluetoothAdapter.STATE_DISCONNECTED) {
                // 蓝牙已断开
                if (listener != null) {
                    listener.onBluetoothDisconnected();
                }
            }
        } else if (BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {
            // A2DP连接状态改变
            int state = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, BluetoothA2dp.STATE_DISCONNECTED);
            
            if (state == BluetoothA2dp.STATE_CONNECTED) {
                // A2DP已连接
                if (listener != null) {
                    listener.onBluetoothConnected();
                }
            } else if (state == BluetoothA2dp.STATE_DISCONNECTED) {
                // A2DP已断开
                if (listener != null) {
                    listener.onBluetoothDisconnected();
                }
            }
        }
    }
    
    /**
     * 注册广播接收器
     * @param context 上下文
     */
    public void register(Context context) {
        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
        filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);
        context.registerReceiver(this, filter);
    }
    
    /**
     * 注销广播接收器
     * @param context 上下文
     */
    public void unregister(Context context) {
        try {
            context.unregisterReceiver(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}